package com.fishing.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import QL.ButlerAnnouncementPublishRequest;
import QL.ButlerAnnouncementPublishResponse;
import QL.QLClient;

import com.alibaba.fastjson.JSON;
import com.fishing.entity.Notice;
import com.fishing.filter.LoginFilter;
import com.fishing.service.NoticeService;

/***
 * 获取在线用户相关接口
 * @author tgh
 *
 */
@Controller
public class NoticeController{
	private Log log = LogFactory.getLog(LoginFilter.class);
	@Autowired
	private NoticeService noticeService;
	
	private static QLClient client = new QLClient();
	
	
	@RequestMapping("notice/toAdd")
	public ModelAndView toAdd(HttpServletRequest request)throws Exception{
		ModelAndView mav = new ModelAndView("notice/add");
		return mav;
	}
	
	
	@ResponseBody
	@RequestMapping(value="notice/add",method = RequestMethod.POST,produces="text/html;charset=UTF-8") //防止页面乱码
	public String add(String title,String contents,String noticeType,HttpServletRequest request) throws Exception{
		ButlerAnnouncementPublishRequest req = new ButlerAnnouncementPublishRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		req.ContentType = Integer.parseInt(noticeType);
		if (req.ContentType == 0) {
			req.TitleCN=title;
		}else {
			req.TitleCN ="CN";
		}
		req.ContentCN=contents;
		log.debug(req.TitleCN + " " + req.ContentCN);
		ButlerAnnouncementPublishResponse rsp = new ButlerAnnouncementPublishResponse();
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		//插入公告记录
		if(rsp.ErrMsg == "" || rsp.ErrMsg == null) {
			Date  date = new Date();
			SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Notice notice = new Notice();
			notice.setTitle(title);
			notice.setContents(contents);
			notice.setAdminName((String)request.getSession().getAttribute("Admintruename"));
			notice.setCreateTime(simpledate.format(date));
			notice.setZoneId((int)request.getSession().getAttribute("ZoneId"));
			this.noticeService.insert(notice);
		}
		return rsp.ErrMsg;
	}
	
	@RequestMapping(value="notice/toList")
	public ModelAndView toList(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("notice/list");
		return mav;
	}
    
	@ResponseBody
	@RequestMapping(value="notice/list",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	public Map list(String PageIndex,String PageCount,HttpServletRequest request) throws Exception {
		List<Notice> notice = this.noticeService.selectAll(PageIndex,PageCount,(int)request.getSession().getAttribute("ZoneId"));
		int count = this.noticeService.selectCount((int)request.getSession().getAttribute("ZoneId"));
		Map map = new HashMap();
		map.put("status", "200");
		map.put("message", "");
		map.put("count", count);
		map.put("data", notice);
		return map;
	}
}
